// builder.cpp: Source for Builder abstract class
// Written by Victor Luna Freire - victorclf@live.com
// Copyright (c) Victor Luna Freire. All rights reserved.
// Sunday, October 25, 2009 10:21:34 PM

#include "builder.h"

// Builds an empty maze with every cell isolated from each other.
void Builder::clear_maze() 
{
	maze.clear();
	maze.push_back(std::string(w * 2 + 1, WALL)); // first line
	for(int y = 0; y < h; ++y) {
		std::string line1(1, WALL);
		for(int x = 0; x < w; ++x) {
			line1 += NO_WALL;
			line1 += WALL;
		}

		std::string line2(w * 2 + 1, WALL);
		
		maze.push_back(line1);
		maze.push_back(line2);
	}
}



// Carve a passage between two cells (cell position) and returns the removed wall position (true pos).
Vec2i Builder::carve(const Vec2i &src_cpos, const Vec2i &dst_cpos) 
{
	Vec2i wall;
	Vec2i src = true_coords(src_cpos);
	Vec2i dst = true_coords(dst_cpos);
	if(src.x == dst.x) {
		wall.x = src.x;
		wall.y = std::min(src.y, dst.y) + 1;
	}
	else {
		wall.x = std::min(src.x, dst.x) + 1;
		wall.y = src.y;
	}
	
	maze[wall.y][wall.x] = NO_WALL;
	return wall;
}
